<template>
<div id="TxSq" class="page-blank">
  <van-nav-bar title="申请提现" left-arrow @click-left="goBack()"/>
  <van-cell-group>    
    <van-field required v-model="form.withdrawalAmount" label="提现金额" :placeholder="`${ $store.state.userInfo.roleType == 3 ? '10-500' : '100-500（含500）' }内，${ $store.state.userInfo.roleType == 3 ? '10' : '100' }的整数倍`"/>
    <van-field required readonly label="账户类型" value="微信"/>
    <van-field v-model="phone.code" required clearable label="验证码" placeholder="请输入验证码">
      <van-button slot="button" size="small" type="primary" @click="phone.fasong">{{ phone.number > 0 ? phone.number + '后再次发送' : '发送验证码' }}</van-button>
    </van-field>
  </van-cell-group>
  <!-- <div class="notice" v-if="$store.state.userInfo.roleType != 1">* 每月1号到19号提现次月5号到账</div>
  <div class="notice" v-if="$store.state.userInfo.roleType != 1">* 每月20号到30号提现次月15号到账</div> -->
  <div class="notice" v-if="$store.state.userInfo.roleType == 1">* 提现申请通过后，1-3天到账</div>
  <!-- <div class="notice">* 每日可提现2次</div> -->
  <div class="notice" v-if="$store.state.userInfo.roleType == 3">* 每天最多提现2次，每月最多提现4次</div>
  <div class="notice" v-else>* 每天最多提现5次</div>
  <div class="notice" v-if="$store.state.userInfo.roleType != 1">* 每次提现5分钟内到账，请注意查收</div>
  <div class="notice" v-if="$store.state.userInfo.roleType != 1">* 微信打款会收取您{{ $store.state.userInfo.roleType == 3 ? '2.5' : '1' }}%的费用，其中包含税费及提现手续费。</div>
  <van-button round type="info" size="small" :loading="submitLoading" @click="submit">提交</van-button>
</div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';
import axios from '@/utils/request';

@Component
export default class TxSq extends Vue {

  form: any = {
    withdrawalWay: "微信",
    withdrawalAmount: ''
  }

  userInfo: any = {}

  submitLoading: any = false

  phone: any = {
    number: 0,
    code: '',
    fasong: () => {
      if(this.phone.number > 0){
        this.$toast(this.phone.number + '秒后可重新发送');
        return ;
      }
      let phone = this.$store.state.userInfo.phone;
      if(!phone) { this.$toast('您尚未绑定手机号，请先绑定手机号！'); return ; }
      this.phone.number = 60;
      setTimeout(() => { this.phone.jishi(); }, 1000);
      axios.get('/api/SmsController/sendverifyCode', { phoneNumber: phone }).then((res: any) => {
        this.$toast('验证码已发送到手机号：' + phone.substring(0,3) + '****' + phone.substring(7,11) );
      }).catch((err: any) => {
        this.$toast('今日验证码发送已达上限，请明天再试');
        this.phone.number = 0;
      })
    },
    jishi: () => {
      if(this.phone.number == 0) return ;
      this.phone.number = this.phone.number - 1;
      if(this.phone.number > 0) setTimeout(() => { this.phone.jishi(); }, 1000);
    }
  }

  submit() {
    if( this.submitLoading ) return ;
    let phone = this.$store.state.userInfo.phone;
    if(!phone) { this.$toast('您尚未绑定手机号，请先绑定手机号！'); return ; }
    let jiner = this.form.withdrawalAmount;
    if(!jiner){ this.$toast('请填写提现金额！'); return ; }
    // let reg=/^([1-2][0-9]00)|([1-9]00)$/;
    // if(!reg.test(jiner)){ this.$toast('提现金额要大于等于100，小于等于2000，100的整数倍！'); return ; }
    if(this.$store.state.userInfo.roleType == 3) {
      let reg=/^(([1-4][0-9]0)|([1-9]0)|([1-5]00))$/;
      if(!reg.test(jiner)) return this.$toast('提现金额要大于等于10，小于等于500，10的整数倍！');
    } else {
      let reg=/^[1-5]00$/;
      if(!reg.test(jiner)) return this.$toast('提现金额要大于等于100，小于等于500，100的整数倍！');
    }
    if(!this.phone.code){ this.$toast('请填写手机验证码！'); return ; }
    this.submitLoading = true;
    axios.get('/api/SmsController/checkSmsCode', { phoneNumber: phone, code: this.phone.code }).then((res: any) => {
      axios.post('/api/WithdrawController/addWithdraw', { operatingWay: 0, userId: this.$store.state.userInfo.userId, withdrawalAmount: jiner, 
        withdrawalWay: 2, wxOpenid: this.$store.state.userInfo.wxOpenId }).then((res: any) => {
        this.$dialog.alert({ title: '提示', message: '提现金额审核通过后会入账到当前微信余额中，请确认。', messageAlign: 'left' }).then(() => {
          this.$router.go(-1);
        });
      }).catch((err: any) => {      
        this.submitLoading = false;
      })
    }).catch((err: any) => {      
      this.submitLoading = false;
      this.$toast('验证码填写有误！');
    })
  }

  goBack() {
    this.$router.go(-1);
  }

  created() {
  }
  
};
</script>

<style lang="scss">
#TxSq {
  .notice {
    background: #ffffff;
    padding: 10px;
    color: #f44;
    line-height: 24px;
    font-size: 14px;
  }
}
</style>

